package com.amigo.online.provider.user.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.provider.user.exception.FileUploadException;

/**
 * <code>UploadImgUtil.java</code>
 * <p>功能:图片上传公共类
 *
 * <p>Copyright 南京阿觅谷 2018 All right reserved.
 *
 * @author LILONG 时间 2018年6月21日 下午3:26:03
 * @version 1.0
 * </br>最后修改人 无
 */
@Component
@PropertySource(value = "classpath:bootstrap.yml")
public class UploadImgUtil {

    private static Logger logger = Logger.getLogger(UploadImgUtil.class);

    /**
     * 功能:
     * <p>作者 LILONG 2018年6月21日 下午3:25:54
     *
     * @param mulfile
     * @param associateId
     * @return
     */
    public static List<String> uploadImg(MultipartFile[] mulfile) {
        List<String> fileIdList = new ArrayList<String>();
        if (mulfile != null && mulfile.length > 0) {
            try {
                for (int i = 0; i < mulfile.length; i++) {
                    if (!mulfile[i].isEmpty()) {
                        String fileid = FastDFSClient.uploadFile(mulfile[i].getBytes(), null);
                        if (StringUtils.isNotEmpty(fileid)) {
                            fileIdList.add(fileid);
                        } else {
                            throw new FileUploadException(StatusCode.FILE_UPLOAD_FILURE);
                        }
                    } else {
                        throw new FileUploadException(StatusCode.NO_FILE_ACQUIRE);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new FileUploadException(StatusCode.FILE_UPLOAD_FILURE);
            }
        }
        return fileIdList;
    }

    /**
     * 上传文件
     *
     * @param inputStream
     * @return
     */
    public static String uploadSingleImgByInputStream(InputStream inputStream) {
        String result = null;
        try {
            byte[] file_buff = null;
            if (inputStream != null) {
                //输入流中可读取字节数
                int len = inputStream.available();
                file_buff = new byte[len];
                inputStream.read(file_buff);
            }
            result = FastDFSClient.uploadFile(file_buff, null);
        } catch (IOException e) {
            logger.error("文件上传失败:" + e);
        } catch (Exception e) {
            logger.error("文件上传失败:" + e);
        }
        return result;
    }

    public static String uploadSingleImgByByteArray(byte[] file_buff) {
        String result = null;
        try {
            result = FastDFSClient.uploadFile(file_buff, null);
        } catch (IOException e) {
            logger.error("文件上传失败:" + e);
        } catch (Exception e) {
            logger.error("文件上传失败:" + e);
        }
        return result;
    }

    public static String uploadSingleImg(MultipartFile file) {
        String result = null;
        try {
            if (!file.isEmpty()) {
                result = FastDFSClient.uploadFile(file.getBytes(), null);
                if (StringUtils.isEmpty(result)) {
                    throw new FileUploadException(StatusCode.FILE_UPLOAD_FILURE);
                }
            } else {
                throw new FileUploadException(StatusCode.NO_FILE_ACQUIRE);
            }
        } catch (Exception e) {
            logger.error("图片上传失败！");
            throw new FileUploadException(StatusCode.FILE_UPLOAD_FILURE);
        }
        return result;
    }

    /**
     * 功能:删除临时文件
     * <p>
     * 作者 LILONG 2018年6月4日 下午2:49:39
     *
     * @param file
     */
    @Autowired
    private static void deleteTempFile(File file) {
        if (file.exists()) {
            file.delete();
        }
    }

    /**
     * 功能:
     * <p>
     * 作者 LILONG 2018年6月4日 下午4:08:39
     *
     * @param ins
     * @param file
     */
    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static int deleteFile(String fileId) {
        return FastDFSClient.deleteFile(fileId);
    }
}